package com.woshua.bitmapmodel.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.IdRes;

import java.io.InputStream;

/**
 * @author Steven.zhao
 * email:hongtu.zhao@goodwinsoft.net
 * date:2019/8/2
 * desc:位图工具类
 */
public class BitmapUtil {

    public static Bitmap ratio(String filePath,int width,int height)
    {
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        options.inPreferredConfig= Bitmap.Config.RGB_565;

        //预加载
        BitmapFactory.decodeFile(filePath,options);

        int originW=options.outWidth;
        int originH=options.outHeight;

        options.inSampleSize=getSampleSize(originW,originH,width,height);
        options.inJustDecodeBounds=false;

        return BitmapFactory.decodeFile(filePath,options);
    }

//    计算采样率
    private static int getSampleSize(int originW, int originH, int width, int height) {
        int sampleSize=1;
        if(originW>originH && originW>width)
        {
            sampleSize=originW/width;
        }
        else if(originW<originH && originH>height)
        {
            sampleSize=originH/height;
        }

        if(sampleSize<=0)
        {
            sampleSize=1;
        }
        return sampleSize;
    }

}
